import { setProfile } from "redux/actions/AuthActions";
import { setCallbackError, setLoading } from "redux/actions/LoadingActions";
import { messageService } from "./messageService";
export const callGetApi = (url, dispatch, callback, callbackError = null, logout = true) => {
  const requestOptions = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    }
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        if (logout) {
          messageService.sendMessage('SignIn');
        }
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      console.log(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

export const callGetNewsApi = (url, dispatch, callback, callbackError = null, logout = true) => {
  const requestOptions = {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    }
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        if (logout) {
          messageService.sendMessage('SignIn');
        }
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      console.log(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

export const callPostApi = (url, param, dispatch, callback, callbackError = null, logout = true) => {
  const requestOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    },
    body: JSON.stringify(param)
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      console.log(res);
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        if (logout) {
          messageService.sendMessage('SignIn');
        }
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      console.log(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

export const callPostNewsApi = (url, param, dispatch, callback, callbackError = null, logout = true) => {
  const requestOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    },
    body: JSON.stringify(param)
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      console.log(res);
      if (res.error) {
        dispatch(setCallbackError(res.error));
      }
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        if (logout) {
          messageService.sendMessage('SignIn');
        }
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      console.log(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

export const callUploadFile = (url, file, dispatch, callback, callbackError = null) => {
  const body = new FormData();
  body.append("file", file);
  const requestOptions = {
    method: 'POST',
    headers: {
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    },
    body: body
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        messageService.sendMessage('SignIn');
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

export const callUploadFileNews = (url, file, dispatch, callback, callbackError = null) => {
  const body = new FormData();
  body.append("image", file.image);
  body.append("back_image", file.back_image);

  const requestOptions = {
    method: 'POST',
    headers: {
      'X-API-KEY': process.env.REACT_APP_API_KEY,
      'Token': localStorage.getItem("token")
    },
    body: body
  };
  dispatch(setLoading(true))
  fetch(`${process.env.REACT_APP_API_URL}${url}`, requestOptions)
    .then(response => response.json())
    .then(res => {
      dispatch(setLoading(false))
      if (res.status == 1000) {
        dispatch(setProfile(null));
        setCallbackError("Token is expired, please login");
        messageService.sendMessage('SignIn');
      }
      else if (res.status == 1) {
        callback(res.data);
      }
      else {
        setCallbackError(res.error);
        if (callbackError) {
          callbackError(res.error)
        }
      }
    })
    .catch((error) => {
      dispatch(setLoading(false));
      setCallbackError(error);
      if (callbackError) {
        callbackError(error)
      }
    });
}

